package com.base.cn.platform.os.controller.statistics.trade;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.statistics.base.StatisticsDateQueryCondition;
import com.base.cn.platform.os.entity.statistics.trade.PayOrderFormNum;
import com.base.cn.platform.os.entity.statistics.trade.PayTypeCountNum;
import com.base.cn.platform.os.entity.statistics.trade.TradePayOrderNum;
import com.base.cn.platform.os.service.order.order.OrderBiz;
import com.base.cn.platform.os.service.order.order.OrderDetailsBiz;
import com.base.cn.platform.os.service.statistics.base.ZStatisticsOrderBaseCountBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 交易分析统计接口Controller
 *
 * @author s.li
 * @date 2019/04/25
 */
@RestController
@RequestMapping("/manage/statistics")
public class TradeStatisticsController extends BaseController {

    @Autowired
    private OrderDetailsBiz orderDetailsBiz;
    @Autowired
    private ZStatisticsOrderBaseCountBiz zStatisticsOrderBaseCountBiz;
    @Autowired
    private OrderBiz orderBiz;

    /**
     * 查看交易分析统计中的交易数据
     * @param dateQueryCondition 查询的时间条件
     * @return Map<String,Object>
     */
    @RequestMapping("/tradePayOrderNumList")
    public Map<String,Object> tradePayOrderNumList(@RequestBody StatisticsDateQueryCondition dateQueryCondition){
        dateQueryCondition.setTimeCondition(new Date());
        StringBuilder timeFormat = new StringBuilder("yyyy-MM-dd");
        //获取时间列表
        List<Date> dateList = new ArrayList<>();
        List<Date> oldDateList = dateQueryCondition.getDayList();
        if(oldDateList.size() ==1){
            timeFormat.delete(0,timeFormat.length());
            timeFormat.append("yyyy-MM-dd HH");
            List<Date> _dateList = DateUtils.getDay24HourList(oldDateList.get(0));
            dateList.addAll(_dateList);
        }else{
            dateList.addAll(oldDateList);
        }
        //获取查询时间的SQL
        String timeSql;
        if(oldDateList.size()==1){
            dateQueryCondition.setType(1);
            timeSql = "('"+DateUtils.format(oldDateList.get(0),"yyyy-MM-dd")+"')";
        }else{
            timeSql = StatisticsDateQueryCondition.getTimeSql(oldDateList,"yyyy-MM-dd");
        }
        List<TradePayOrderNum> tradePayOrderNumList = new ArrayList<>();
        //查询数据
        List<TradePayOrderNum> _tradePayOrderNumList = orderDetailsBiz.tradePayOrderNumList(dateQueryCondition.getType(),timeSql);
        //如果有数据
        if(ObjectUtils.isNotEmpty(_tradePayOrderNumList)){
            Map<String,TradePayOrderNum> tradePayOrderNumMap = _tradePayOrderNumList.stream().collect(Collectors.toMap(e-> DateUtils.format(e.getCountTime(),timeFormat.toString()), e->e));
            dateList.forEach(d->{
                String key = DateUtils.format(d,timeFormat.toString());
                TradePayOrderNum t = tradePayOrderNumMap.get(key);
                if(t==null){//如果某天的数据没有，则生成一条默认的数据
                    t = new TradePayOrderNum();
                    t.setCountTime(d);
                    _tradePayOrderNumList.add(t);
                }
            });
            tradePayOrderNumList.addAll(_tradePayOrderNumList);
        }else{//如果没有，则给每一天初始化一条默认数据
            dateList.forEach(d->{
                TradePayOrderNum t = new TradePayOrderNum();
                t.setCountTime(d);
                tradePayOrderNumList.add(t);
            });
        }
        //重新排序
        Collections.sort(tradePayOrderNumList, new Comparator<TradePayOrderNum>() {
            @Override
            public int compare(TradePayOrderNum o1, TradePayOrderNum o2) {
                return o1.getCountTime().after(o2.getCountTime())? 1:-1;
            }
        });
        Map<String,Object> resultMap = ResultUtil.SUCCESS(tradePayOrderNumList);
        /**总订单量*/
        int orderNum = tradePayOrderNumList.stream().mapToInt(TradePayOrderNum::getOrderNum).sum();
        resultMap.put("orderNum",orderNum);
        /**录播课程订单量*/
        int videoOrderNum = tradePayOrderNumList.stream().mapToInt(TradePayOrderNum::getVideoOrderNum).sum();
        resultMap.put("videoOrderNum",videoOrderNum);
        /**专栏课程订单量*/
        int columnsOrderNum = tradePayOrderNumList.stream().mapToInt(TradePayOrderNum::getColumnsOrderNum).sum();
        resultMap.put("columnsOrderNum",columnsOrderNum);
        /**套餐订单量*/
        int packageOrderNum = tradePayOrderNumList.stream().mapToInt(TradePayOrderNum::getPackageOrderNum).sum();
        resultMap.put("packageOrderNum",packageOrderNum);
        /**直播课程订单量*/
        int liveOrderNum = tradePayOrderNumList.stream().mapToInt(TradePayOrderNum::getLiveOrderNum).sum();
        resultMap.put("liveOrderNum",liveOrderNum);
        /**试卷订单量*/
        int examOrderNum = tradePayOrderNumList.stream().mapToInt(TradePayOrderNum::getExamOrderNum).sum();
        resultMap.put("examOrderNum",examOrderNum);
        /**图上订单量*/
        int bookOrderNum = tradePayOrderNumList.stream().mapToInt(TradePayOrderNum::getBookOrderNum).sum();
        resultMap.put("bookOrderNum",bookOrderNum);
        /**会员商品订单量*/
        int memberOrderNum = tradePayOrderNumList.stream().mapToInt(TradePayOrderNum::getMemberOrderNum).sum();
        resultMap.put("memberOrderNum",memberOrderNum);
        /**问答订单量*/
        int qaOrderNum = tradePayOrderNumList.stream().mapToInt(TradePayOrderNum::getQaOrderNum).sum();
        resultMap.put("qaOrderNum",qaOrderNum);
        /**围观订单量*/
        int attendOrderNum = tradePayOrderNumList.stream().mapToInt(TradePayOrderNum::getAttendOrderNum).sum();
        resultMap.put("attendOrderNum",attendOrderNum);
        resultMap.put("type",dateQueryCondition.getType());
        return resultMap;
    }

    /**
     * 查询获取交易额度与订单量分布
     * @return Map<String,Object>
     */
    @RequestMapping("/queryOrderTradeSpreadList")
    public Map<String,Object> queryOrderTradeSpreadList(@RequestBody StatisticsDateQueryCondition dateQueryCondition){
        dateQueryCondition.setTimeCondition(new Date());
        //获取时间列表
        List<Date> dateList = dateQueryCondition.getDayList();
        String timeSql = StatisticsDateQueryCondition.getTimeSql(dateList,"yyyy-MM-dd");
        OrderTradeSpread[] arr = OrderTradeSpread.values();
        List<Map<String,Object>> _resultMapList =new ArrayList<>();
        List<Map<String,Object>> orderTradeSpreadList = orderBiz.queryOrderTradeSpreadList(timeSql);
        if(ObjectUtils.isNotEmpty(orderTradeSpreadList)){
            Map<String,Map<String,Object>> maps = orderTradeSpreadList.stream().collect(Collectors.toMap(e->e.get("stageName").toString(),e->e));
            Arrays.stream(arr).forEach(e->{
                Map<String,Object> map = maps.get(e.getStageName());
                if(map==null){
                    map = new HashMap<>();
                    map.put("stageName",e.getStageName());
                    map.put("orderNum",0);
                }
                map.put("showName",e.getShowName());
                _resultMapList.add(map);
            });
        }else{
            Arrays.stream(arr).forEach(e->{
                Map<String,Object> map = new HashMap<>();
                map.put("stageName",e.getStageName());
                map.put("showName",e.getShowName());
                map.put("orderNum",0);
                _resultMapList.add(map);
            });
        }
        List<Map<String,Object>> resultMapList =new ArrayList<>();
        Map<String,Map<String,Object>> mapMap = _resultMapList.stream().collect(Collectors.toMap(e->e.get("stageName").toString(),e->e));
        Arrays.stream(arr).forEach(obj->{
            resultMapList.add(mapMap.get(obj.getStageName()));
        });
        return ResultUtil.SUCCESS(resultMapList);
    }

    /**
     * 查询订单支付方式占比统计数据
     * @return Map<String,Object>
     */
    @RequestMapping("/queryOrderPayTypeOrderNum")
    public Map<String,Object> queryOrderPayTypeOrderNum(@RequestBody StatisticsDateQueryCondition dateQueryCondition){
        dateQueryCondition.setTimeCondition(new Date());
        //获取时间列表
        List<Date> dateList = dateQueryCondition.getDayList();
        String timeSql = StatisticsDateQueryCondition.getTimeSql(dateList,"yyyy-MM-dd");
        PayTypeCountNum payTypeCountNum = orderBiz.queryOrderPayTypeOrderNum(timeSql);
        return ResultUtil.SUCCESS(payTypeCountNum);
    }

    /**
     * 查询订单的支付来源数据统计
     * @return Map<String,Object>
     */
    @RequestMapping("/queryPayOrderFormNumData")
    public Map<String,Object> queryPayOrderFormNumData(@RequestBody StatisticsDateQueryCondition dateQueryCondition){
        dateQueryCondition.setTimeCondition(new Date());
        //获取时间列表
        List<Date> dateList = dateQueryCondition.getDayList();
        String timeSql = StatisticsDateQueryCondition.getTimeSql(dateList,"yyyy-MM-dd");
        PayOrderFormNum payOrderFormNum = orderBiz.queryPayOrderFormNumData(timeSql);
        return ResultUtil.SUCCESS(payOrderFormNum);
    }
}
